package cn.felord.common.controller;

import cn.felord.common.bean.Rest;
import cn.felord.common.bean.RestBody;
import cn.felord.common.entity.SysRole;
import cn.felord.common.security.JwtClaims;
import cn.felord.common.security.JwtClaimsBuilder;
import cn.felord.common.security.JwtTokenGenerator;
import cn.felord.common.service.ISysRoleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * The type Test controller.
 *
 * @author Dax
 * @since 23 :24  2018/9/15
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private ISysRoleService iSysRoleService;
    @Resource
    private JwtTokenGenerator jwtTokenGenerator;


    /**
     * Test rest.
     *
     * @param msg the msg
     * @return the rest
     * @throws JsonProcessingException the json processing exception
     */
    @GetMapping("/{msg}")
    public Rest test(@PathVariable String msg) throws JsonProcessingException {
        JwtClaimsBuilder jwtClaimsBuilder = new JwtClaimsBuilder();

        JwtClaims builder = jwtClaimsBuilder
                .iss("tara")
                .sub("all")
                .userId("1123123123")
                .expDays(30)
                .builder();
        String s = jwtTokenGenerator.jwtToken(builder);
        Map<String, Object> map = new HashMap<>();
        map.put("token", s);
        return RestBody.okData(map, msg);
    }

    /**
     * Add role rest.
     *
     * @param sysRole the sys role
     * @return the rest
     */
    @GetMapping("/add/role")
    public Rest addRole(SysRole sysRole) {
        Integer integer = iSysRoleService.saveRole(sysRole);
        return RestBody.ok();
    }


}
